//
//  HQTool.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/12.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQTool.h"
#import "QSKeyChain.h"

@implementation HQTool
/**
 *  校验手机号格式是否正确
 *
 *  @return 校验结果
 */
+(BOOL)phoneNoRegex:(NSString *)phoneNo{
    NSString *phoneRegex = @"^[1][0-9]\\d{9}$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phonePredicate evaluateWithObject:phoneNo];
}

-(void)startVerify:(UIButton *)button{
    _sec = kAuthCodeInvalidTime;
    verifyTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target:self
                                                 selector:@selector(timerActionForVerify:)
                                                 userInfo:@{@"button":button} repeats:YES];
    button.userInteractionEnabled = NO;
}

-(void)timerActionForVerify:(NSTimer *)timer{
    UIButton *_getAuthCodeItem = timer.userInfo[@"button"];
    _sec--;
    [_getAuthCodeItem setTitleColor:[self grayLightColor] forState:UIControlStateNormal];
    _getAuthCodeItem.backgroundColor = [UIColor whiteColor];
    NSString *secTime = [NSString stringWithFormat:@"%i秒后获取",_sec];
    [_getAuthCodeItem setTitle:secTime forState:UIControlStateNormal];
    if (_sec==0) {
        _getAuthCodeItem.userInteractionEnabled = YES;
        [_getAuthCodeItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _getAuthCodeItem.backgroundColor = [self colorHexValue:0x007AFF];
        [_getAuthCodeItem setTitle:@"重新获取" forState:UIControlStateNormal];
        [verifyTimer invalidate];
        _sec = kAuthCodeInvalidTime;
    }
}

/**
 获取手机唯一标识
 
 @return 获取手机唯一标识
 */
#define kEWalletUUID @"LBRS_UUID"
#define kAccountName @"com.buyi.lbrs"
+(NSString *)phoneUUID{
    NSMutableDictionary *userDataKeyChain = (NSMutableDictionary *)[QSKeyChain load:kAccountName];
    if([userDataKeyChain objectForKey:kEWalletUUID]){
        return [userDataKeyChain objectForKey:kEWalletUUID];
    }else{
        NSString *clientId = [self uuidString];
        NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];
        [usernamepasswordKVPairs setObject:clientId forKey:kEWalletUUID];
        [QSKeyChain save:kAccountName data:usernamepasswordKVPairs];
        return clientId;
    }
}

+ (NSString *)uuidString{
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];
    CFRelease(uuid_ref);
    CFRelease(uuid_string_ref);
    NSString *uuidStr = [uuid lowercaseString];
    uuidStr = [uuidStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
    return uuidStr;
}

/**
 获取当前时间戳

 @return 获取时间戳
 */
+ (NSString *)currentTimeStr{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970];// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}


@end
